package name.matthewgreet.example10.view; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; import name.matthewgreet.example10.dto.DateRange; import name.matthewgreet.example10.dto.PrimeMinisterDTO; import name.matthewgreet.example10.dto.PrimeMinisterDTO.Party; import name.matthewgreet.strutscommons.annotation.CustomConversion; import name.matthewgreet.strutscommons.util.StrutsMiscellaneousLibrary; import name.matthewgreet.strutscommons.policy.AbstractCustomFormatterSupport; public class PrimeMinisterDisplay { public static class PartyFormatter extends AbstractCustomFormatterSupport<Party> { @Override public Class<Party> getRecipientClass() { return Party.class; } @Override public String format(Party unformattedValue) { return PrimeMinisterDisplay.formatParty(unformattedValue); } } private String id; private String name; private String dateOfBirth; private List<String> termsOfOffice; @CustomConversion(validatorClass = PartyFormatter.class) private String party; private String maxSeatsWon; public String formatDateRange(DateRange unformattedValue) { DateFormat dateFormat; String result; dateFormat = new SimpleDateFormat("dd/MM/yyyy"); result = ""; if (unformattedValue.getStart() != null) { result = result + dateFormat.format(unformattedValue.getStart()); } result = result + "-"; if (unformattedValue.getEnd() != null) { result = result + dateFormat.format(unformattedValue.getEnd()); } return result; } public static String formatParty(Party party) { switch (party) { case CONSERVATIVE: return "Conservative"; case LABOUR: return "Labour"; case LIBERAL: return "Liberal"; case WHIG: return "Whig"; } return ""; } public PrimeMinisterDisplay(PrimeMinisterDTO model) { StrutsMiscellaneousLibrary.updateDisplay(PrimeMinisterDTO.class, model, this); termsOfOffice = new ArrayList<>(); if (model != null) { for (DateRange termOfOffice: model.getTermsOfOffice()) { termsOfOffice.add(formatDateRange(termOfOffice)); } } } public String getId() { return id; } public String getName() { return name; } public String getDateOfBirth() { return dateOfBirth; } public List<String> getTermsOfOffice() { return termsOfOffice; } public String getParty() { return party; } public String getMaxSeatsWon() { return maxSeatsWon; } @Override public String toString() { final int maxLen = 30; return "PrimeMinisterDisplay [id=" + id + ", name=" + name + ", dateOfBirth=" + dateOfBirth + ", termsOfOffice=" + (termsOfOffice != null ? termsOfOffice.subList(0, Math.min(termsOfOffice.size(), maxLen)) : null) + ", party=" + party + ", maxSeatsWon=" + maxSeatsWon + "]"; } }